﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Uber.Collections;
using Uber.Storage;
using Uber.Messages;
using Uber.HabboHotel.GameClients;

namespace Uber.HabboHotel.Catalogs
{
    class CatalogPage
    {
        private int Id;
        public int ParentId;

        public string Caption;

        public bool Visible;
        public bool Enabled;
        public bool ComingSoon;

        public uint MinRank;
        public bool ClubOnly;

        public int IconColor;
        public int IconImage;

        public string Layout;

        public string LayoutHeadline;
        public string LayoutTeaser;
        public string LayoutSpecial;

        public string Text1;
        public string Text2;
        public string TextDetails;
        public string TextTeaser;

        public SafeList<CatalogItem> Items;

        public int PageId
        {
            get
            {
                return Id;
            }
        }

        public CatalogPage(int mId, int mParentId, string mCaption, bool mVisible, bool mEnabled,
            bool mComingSoon, uint mMinRank, bool mClubOnly, int mIconColor, int mIconImage, string mLayout, string mLayoutHeadline,
            string mLayoutTeaser, string mLayoutSpecial, string mText1, string mText2, string mTextDetails,
            string mTextTeaser)
        {
            this.Items = null;
            this.Items = new SafeList<CatalogItem>();

            this.Id = mId;
            this.ParentId = mParentId;
            this.Caption = mCaption;
            this.Visible = mVisible;
            this.Enabled = mEnabled;
            this.ComingSoon = mComingSoon;
            this.MinRank = mMinRank;
            this.ClubOnly = mClubOnly;
            this.IconColor = mIconColor;
            this.IconImage = mIconImage;
            this.Layout = mLayout;
            this.LayoutHeadline = mLayoutHeadline;
            this.LayoutTeaser = mLayoutTeaser;
            this.LayoutSpecial = mLayoutSpecial;
            this.Text1 = mText1;
            this.Text2 = mText2;
            this.TextDetails = mTextDetails;
            this.TextTeaser = mTextTeaser;

            DataTable Data = null;

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                Data = dbClient.ReadDataTable("SELECT id,item_ids,catalog_name,cost_credits,cost_pixels,amount FROM catalog_items WHERE page_id = '" + Id + "' ORDER BY item_ids ASC");
            }

            if (Data != null)
            {
                foreach (DataRow Row in Data.Rows)
                {
                    if (Row["item_ids"].ToString() == "" || (int)Row["amount"] <= 0)
                    {
                        continue;
                    }

                    Items.Add(new CatalogItem((uint)Row["id"], (string)Row["catalog_name"], (string)Row["item_ids"], (int)Row["cost_credits"], (int)Row["cost_pixels"], (int)Row["amount"]));
                }
            }
        }

        public CatalogItem GetItem(uint Id)
        {
            for(int i = 0; i < Items.Count; i++)
            {
                CatalogItem Item = Items[i]; if (Item == null) { continue; }

                if (Item.Id == Id)
                {
                    return Item;
                }
            }

            return null;
        }

        public void Serialize(GameClient Session, ServerMessage Message)
        {
            Message.AppendBoolean(Visible);
            Message.AppendInt32(IconColor);
            Message.AppendInt32(IconImage);
            Message.AppendInt32(Id);
            Message.AppendStringWithBreak(Caption);
            Message.AppendInt32(UberEnvironment.GetGame().GetCatalog().GetTreeSize(Session, Id));
        }
    }
}
